//
//  XSThumbnailCollectionViewCell.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/9/16.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSThumbnailCollectionViewCell.h"

static inline CAKeyframeAnimation * GetBtnStatusChangedAnimation() {
    CAKeyframeAnimation *animate = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animate.duration = 0.3;
    animate.removedOnCompletion = YES;
    animate.fillMode = kCAFillModeForwards;
    
    animate.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.7, 0.7, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)],
                       [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    return animate;
}

@interface XSThumbnailCollectionViewCell ()

@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, assign) PHImageRequestID imageRequestID;

@end

@implementation XSThumbnailCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [_selectBtn setBackgroundImage:[UIImage imageNamed:@"zl_btn_unselected"] forState:UIControlStateNormal];
    [_selectBtn setBackgroundImage:[UIImage imageNamed:@"zl_btn_selected"] forState:UIControlStateSelected];
    [_selectBtn addTarget:self action:@selector(btnSelectClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnSelectClick:(UIButton *)sender {
    if (!self.photoModel.isSelected) {
        [self.selectBtn.layer addAnimation:GetBtnStatusChangedAnimation() forKey:nil];
    }

    //不在cell中选中, 
//    self.photoModel.selected = !self.photoModel.isSelected;
//    self.selectBtn.selected = self.photoModel.isSelected;
  
    if (self.selectBtnClickedBlock) {
        self.selectBtnClickedBlock(self.selectBtn.isSelected);//now state
    }
}

- (void)setPhotoModel:(XSPhotoModel *)photoModel{
    _photoModel = photoModel;
    
    if (photoModel.type == XSAssetMediaTypeVideo) {
        self.videoImageV.hidden = NO;
        self.timeLabel.hidden = NO;
        self.timeLabel.text = photoModel.duration;
        
        if (photoModel.avasset == nil) {
            [XSPhotosManager requestVideoAVAsset_ForAsset:photoModel.asset progressHandler:nil completion:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary *info) {
                photoModel.avasset = asset;

            }];
         
        }
 
    }else{
        self.videoImageV.hidden = YES;
        self.timeLabel.hidden = YES;
    }
    
    self.selectBtn.selected = photoModel.isSelected;
    

    //扩大点击区域
    [_selectBtn setEnlargeEdgeWithTop:0 right:0 bottom:20 left:20];

        
    //image
    if (photoModel.asset && self.imageRequestID >= PHInvalidImageRequestID) {
        [[PHCachingImageManager defaultManager] cancelImageRequest:self.imageRequestID];
    }
    self.identifier = photoModel.asset.localIdentifier;
    self.thumbnailImageV.image = nil;
    self.imageRequestID = [XSPhotosManager requestImageForAsset:photoModel.asset size:self.thumbnailImageV.bounds.size progressHandler:nil completion:^(UIImage *image, NSDictionary *info) {
        
        if ([self.identifier isEqualToString:photoModel.asset.localIdentifier]) {
            self.thumbnailImageV.image = image;
            photoModel.image = image;
        }
        
        if (![[info objectForKey:PHImageResultIsDegradedKey] boolValue]) {
            self.imageRequestID = -1;
        }
    }];
    
    
}

@end
